﻿using DotNetty.Buffers;
using QQ.Client;
using QQ.Codecs;
using static QQ.Codecs.DotNettyByteBufferExtensions.LengthType;

namespace QQ.Packet.Request
{
    public class UniRequest : IRequest
    {
        public string cmdName { get; set; }
        public IByteBuffer extraData { get; set; }
        public IByteBuffer body { get; set; }
        private readonly LoginSigInfo loginSigInfo;
        public UniRequest(LoginSigInfo loginSigInfo)
        {
            this.loginSigInfo = loginSigInfo;
        }

        public IByteBuffer Serialize()
        {
            IByteBuffer head = Unpooled.Buffer()
                .WriteBytesWithLength(cmdName.ToByteBuffer(), INT, lengthOffset: 4)
                .WriteBytesWithLength(loginSigInfo.outgoingPacketSessionId, INT, lengthOffset: 4)
                .WriteBytesWithLength(extraData == null ? Unpooled.Buffer() : extraData, INT, lengthOffset: 4);
            extraData = null;//clear the data
            return Unpooled.Buffer()
                .WriteBytesWithLength(head, INT, lengthOffset: 4)
                .WriteBytesWithLength(body, INT, lengthOffset: 4);
        }
    }
}